#ifndef __LINKEDLIST__
#define __LINKEDLIST__

template <class ListElem>
class List
{
public:
     List();
     void insert(const ListElem &elem);          //Inserts a new element
     bool first(ListElem &elem);     //Goes to the first element returns the value
     bool first();                    //Goes to the first element in the list
     bool next(ListElem &elem);     //Goes to next element and writes the element in parameter
     bool next();          //Goes to next element in the list
     bool Change(const ListElem &newElem);     // changes the current element
     bool Remove();      //Removes current element
     bool RemoveF();          //Removes first element

private:
     struct Node;          //Structure a Node
     typedef Node *Link; //Link is a pointer to a Node
     struct Node {          //Define Node
          ListElem elem;     //Element storage
          Link next;          //Pointer to the Next Node
     };
     Link head;               //Head of the list
     Link tail;               //Tail of the list
     Link current;          //Current Position
};
template <class ListElem>
List<ListElem>::List()  //Constructor sets all to 0
{
     head = 0;
     tail = 0;
     current = 0;
}
template<class ListElem>
void List<ListElem>::insert(const ListElem &elem) //Inserts a new Element
{
     Link addedNode (new Node);
     addedNode->elem = elem;
     if (head == NULL)               //Checks if there is not head if so
          head = addedNode;     //makes the new element the head
     else                         //if not then
          tail->next = addedNode;
     tail = addedNode;          //new element is set to the tail
     addedNode->next = NULL;     //Next pointer is set to NULL
}
template<class ListElem>
bool List<ListElem>::first()//Sets the current to the head(first element)
{
     current = head;
     return true;
}
template<class ListElem>
bool List<ListElem>::first(ListElem &elem)     //Sets the current to the head and returns the elem
{
     if(head == 0)
          return false;
     else
     {
          elem = head->elem;          //elem is set to the head's element
          current = head;               //Current is set to head
          return true;
     }
}

template<class ListElem>
bool List<ListElem>::next(ListElem &elem)     //Moves one forward in the list and returns the elem
{
     if(current->next ==0)     //checks if it is the last element
          return false;
     else
     {
          current = current->next;     //sets current to the next elem
          elem = current->elem;          //sets elem to the element in the current
          return true;
     }
}

template<class ListElem>
bool List<ListElem>::next()     //Just moves one forward in the list
{
     if(current->next == 0)     //If this is the tail return false
          return false;
     else
     {
          current = current->next;     //Else move forward
          return true;
     }
}

template<class ListElem>
bool List<ListElem>::Remove()     //Removes the current object in the list
{
     Link NEXT;
     NEXT = current->next;
     current->next = NEXT->next;
     while(current->next != 0)
     {
          current = current->next;
     }
     return true;
}
template<class ListElem>
bool List<ListElem>::RemoveF()     //Removes the first object in the list
{
     head = head->next;
     current->next = head->next;
     while(current->next != 0)
     {
          current = current->next;
     }
     return true;
}
template<class ListElem>
bool List<ListElem>::Change(const ListElem &newElem)     //Changes the object
{
     current->elem = newElem;
     return true;
}

#endif
